
# create combined ESS dataset of migration attitude means from 2012 to 2022

### mig variable ###
bydate6 <- ESS6 %>%
  group_by(date) %>%
  filter(n()>9) %>%
  summarize(mean = mean(mig, na.rm = TRUE),
            sd   = sd(mig),
            mean_p2sd = mean + 2 * sd,
            mean_m2sd = mean - 2 * sd) %>%
  ungroup()

bydate7 <- ESS7 %>%
  group_by(date) %>%
  filter(n()>9) %>%
  summarize(mean = mean(mig, na.rm = TRUE),
            sd   = sd(mig),
            mean_p2sd = mean + 2 * sd,
            mean_m2sd = mean - 2 * sd) %>%
  ungroup()

bydate8 <- ESS8 %>%
  group_by(date) %>%
  filter(n()>9) %>%
  summarize(mean = mean(mig, na.rm = TRUE),
            sd   = sd(mig),
            mean_p2sd = mean + 2 * sd,
            mean_m2sd = mean - 2 * sd) %>%
  ungroup()

bydate9 <- ESS9 %>%
  group_by(date) %>%
  filter(n()>9) %>%
  summarize(mean = mean(mig, na.rm = TRUE),
            sd   = sd(mig),
            mean_p2sd = mean + 2 * sd,
            mean_m2sd = mean - 2 * sd) %>%
  ungroup()

bydate10 <- ESS10 %>%
  group_by(date) %>%
  filter(n()>9) %>%
  summarize(mean = mean(mig, na.rm = TRUE),
            sd   = sd(mig),
            mean_p2sd = mean + 2 * sd,
            mean_m2sd = mean - 2 * sd) %>%
  ungroup()

migovertime <- bind_rows(bydate6, bydate7, bydate8, bydate9, bydate10)

# plot migration attitudes from 2012 to 2022
ggplot(migovertime, aes(x = date / 365 + 1970, y = mean)) + 
  geom_line(size = 1, color = "slateblue4") +
  ylim(0,10) +
  xlab("Year") +
  ylab("High Values = Pro Immigration") +
  theme_light(base_size = 40)


### Immigrant Attitude Factor ###

fac6 <- data61 %>%
  group_by(date) %>%
  filter(n()>9) %>%
  summarize(mean = mean(factor, na.rm = TRUE)) %>%
  ungroup()

fac7 <- data71 %>%
  group_by(date) %>%
  filter(n()>9) %>%
  summarize(mean = mean(factor, na.rm = TRUE)) %>%
  ungroup()

fac8 <- data81 %>%
  group_by(date) %>%
  filter(n()>9) %>%
  summarize(mean = mean(factor, na.rm = TRUE)) %>%
  ungroup()

fac9 <- data91 %>%
  group_by(date) %>%
  filter(n()>9) %>%
  summarize(mean = mean(factor, na.rm = TRUE)) %>%
  ungroup()

fac10 <- data101 %>%
  group_by(date) %>%
  filter(n()>9) %>%
  summarize(mean = mean(factor, na.rm = TRUE)) %>%
  ungroup()

facplot <- bind_rows(fac6, fac7, fac8, fac9, fac10)

# plot migrant attitudes from 2012 to 2022
ggplot(facplot, aes(x = date / 365 + 1970, y = mean)) + 
  geom_line(size = 1, color = "slateblue4") +
  ylim(-9,9) +
  scale_x_continuous(limits = c(2012, 2023), breaks = c(2012, 2013, 2014, 2015, 2016, 2017, 
                                                        2018, 2019, 2020, 2021, 2022)) +
  xlab("Year") +
  ylab("Factor Value") +
  theme_light(base_size = 40)



### Immigration Attitude Factor ###

fac6b <- data62 %>%
  group_by(date) %>%
  filter(n()>9) %>%
  summarize(mean = mean(factor, na.rm = TRUE)) %>%
  ungroup()

fac7b <- data72 %>%
  group_by(date) %>%
  filter(n()>9) %>%
  summarize(mean = mean(factor, na.rm = TRUE)) %>%
  ungroup()

fac8b <- data82 %>%
  group_by(date) %>%
  filter(n()>9) %>%
  summarize(mean = mean(factor, na.rm = TRUE)) %>%
  ungroup()

fac9b <- data92 %>%
  group_by(date) %>%
  filter(n()>9) %>%
  summarize(mean = mean(factor, na.rm = TRUE)) %>%
  ungroup()

fac10b <- data102 %>%
  group_by(date) %>%
  filter(n()>9) %>%
  summarize(mean = mean(factor, na.rm = TRUE)) %>%
  ungroup()

facplot <- bind_rows(fac6b, fac7b, fac8b, fac9b, fac10b)

# plot migration attitudes from 2012 to 2022
ggplot(facplot, aes(x = date / 365 + 1970, y = mean)) + 
  geom_line(size = 1, color = "slateblue4") +
  ylim(-3,3) +
  scale_x_continuous(limits = c(2012, 2023), breaks = c(2012, 2013, 2014, 2015, 2016, 2017, 
                                                        2018, 2019, 2020, 2021, 2022)) +
  xlab("Year") +
  ylab("Factor Value") +
  theme_light(base_size = 40)
